const { ccclass, property } = cc._decorator;
declare let eManager,dbh,DBManager,i18n;
import BaseView from "../../component/BaseView";
import UIHelper = require("../../component/UIHelper");

import DrawOne from "../DrawOne";
@ccclass
export default class RepairAcneView extends BaseView {
    @property(cc.Node)
    bg: cc.Node = null;
    //鼻子粉刺
    @property(DrawOne)
    noseAcne: DrawOne = null;
    //下巴粉刺
    @property(DrawOne)
    chinAcne: DrawOne = null;
    //面膜
    @property(cc.Node)
    mianmoNode: cc.Node = null;
    //鼻子贴面膜选中框
    @property(cc.Node)
    noseSelect: cc.Node = null;
    //鼻子面膜spine
    @property(sp.Skeleton)
    noseMianMoSpine: sp.Skeleton = null;
    //下巴贴面膜选中框
    @property(cc.Node)
    chinSelect: cc.Node = null;
    //下巴面膜spine
    @property(sp.Skeleton)
    chinMianMoSpine: sp.Skeleton = null;
    //祛痘针
    @property(cc.Node)
    zhenNode: cc.Node = null;
    //祛痘针碰撞区
    @property(cc.Node)
    zhenPengNode: cc.Node = null;
    //祛痘区域1
    @property(cc.Node)
    acneNode1: cc.Node = null;
    //祛痘区域2
    @property(cc.Node)
    acneNode2: cc.Node = null;
    //祛痘spine1
    @property(sp.Skeleton)
    acneSpine1: sp.Skeleton = null;
    //祛痘spine2
    @property(sp.Skeleton)
    acneSpine2: sp.Skeleton = null;
    //撕鼻子面膜区域
    @property(cc.Node)
    noseArea: cc.Node = null;
    //撕下巴面膜区域
    @property(cc.Node)
    chinArea: cc.Node = null;
    //棉签
    @property(cc.Node)
    mianQianNode: cc.Node = null;
    @property(cc.Node)
    endNode: cc.Node = null;

    @property(cc.Node)
    noseSiTipsNode: cc.Node = null;
    @property(cc.Node)
    acneSiTipsNode: cc.Node = null;
    @property(cc.Node)
    btnClose: cc.Node = null;

    _point = 1;
    _isPlay = false;
    _startPosx = null;
    _isStart = false;
    onLoad() {
        super.onLoad();
        this.bg.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.bg.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.bg.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.noseSelect.active = true;
        this.mianmoNode.active = true;
        //@ts-ignore
        this._isStart = this.node.openParam;
    };

    onClose(){
    };

    start() {
        super.start();
        this.btnClose.active = !this._isStart;
    };

    onTouchStart(event) {
        if(this._isPlay)
            return;
        let worldPoint = event.touch.getLocation();
        switch (this._point) {
            case 1://贴鼻子面膜
                let localPoint = this.mianmoNode.parent.convertToNodeSpaceAR(worldPoint);
                this.mianmoNode.x = localPoint.x;
                this.mianmoNode.y = localPoint.y;
                break;
            case 2://贴下巴面膜
                let localPoint2 = this.mianmoNode.parent.convertToNodeSpaceAR(worldPoint);
                this.mianmoNode.x = localPoint2.x;
                this.mianmoNode.y = localPoint2.y;
                break;
            case 3://祛痘1
                let localPoint3 = this.zhenNode.parent.convertToNodeSpaceAR(worldPoint);
                this.zhenNode.x = localPoint3.x;
                this.zhenNode.y = localPoint3.y;
                break;
            case 4://祛痘2
                let localPoint4 = this.zhenNode.parent.convertToNodeSpaceAR(worldPoint);
                this.zhenNode.x = localPoint4.x;
                this.zhenNode.y = localPoint4.y;
                break;
            case 5://撕鼻子面膜
                if(this.noseArea.getBoundingBoxToWorld().contains(worldPoint)){
                    this._startPosx = worldPoint;
                }else{
                    this._startPosx = null;
                }
                break;
            case 6://撕下巴面膜
                if(this.chinArea.getBoundingBoxToWorld().contains(worldPoint)){
                    this._startPosx = worldPoint;
                }else{
                    this._startPosx = null;
                }
                break;
            case 7://清理鼻子
                this.noseAcne._touchStart(event,cc.v2(-100,100));
                break;
            case 8://清理下巴
                this.chinAcne._touchStart(event,cc.v2(-100,100));
                break;
        }
        
    };

    onTouchMove(event) {
        if(this._isPlay)
            return;
        let worldPoint = event.touch.getLocation();
        switch (this._point) {
            case 1:
                this.dealOnePoint(worldPoint);
                break;
            case 2:
                this.dealTwoPoint(worldPoint);
                break;
            case 3:
                this.dealThreePoint(worldPoint);
                break;
            case 4:
                this.dealFourPoint(worldPoint);
                break;
            case 7:
                this.noseAcne._touchMove(event);
                break;
            case 8:
                this.chinAcne._touchMove(event);
                break;
        }
        
    };

    onTouchEnd(event) {
        let endPosx = event.touch.getLocation();
        switch (this._point) {
            case 5:
                if (this._startPosx) {
                    if ((endPosx.x - this._startPosx.x) > 50) {
                        this.noseMianMoSpine.setAnimation(0, "02", false);
                        this.noseMianMoSpine.setCompleteListener(() => {
                            this._point++;
                            this._isPlay = false;
                            this.noseSelect.active = false;
                            this.noseSiTipsNode.active = false;
                            this.acneSiTipsNode.active = true;
                            this.chinSelect.active = true;
                        });
                    }
                }
                break;
            case 6:
                if (this._startPosx) {
                    if ((endPosx.x - this._startPosx.x) > 50) {
                        this.chinMianMoSpine.setAnimation(0, "02", false);
                        this.chinMianMoSpine.setCompleteListener(() => {
                            this._point++;
                            this._isPlay = false;
                            this.noseSelect.active = true;
                            this.chinSelect.active = false;
                            this.acneSiTipsNode.active = false;
                            this.mianQianNode.active = true;
                        });
                    }
                }
                break;
            case 7:
                this.noseAcne._touchEnd(() => {
                    this._point++;
                    this.noseSelect.active = false;
                    this.chinSelect.active = true;
                    this.mianQianNode.x = 300;
                    this.mianQianNode.y = -400;
                });
                break;
            case 8:
                this.chinAcne._touchEnd(() => {
                    this.chinSelect.active = false;
                    this.mianQianNode.active = false;
                    this.endNode.active = true;
                });
                break;
        }
        
    };
    //鼻子贴面膜
    dealOnePoint(worldPoint){
        let localPoint = this.mianmoNode.parent.convertToNodeSpaceAR(worldPoint);
        this.mianmoNode.x = localPoint.x;
        this.mianmoNode.y = localPoint.y;
        let peng = this.noseSelect.getBoundingBoxToWorld();
        let box = this.mianmoNode.getBoundingBoxToWorld();
        if (peng.intersects(box)) {
            this._isPlay = true;
            this.mianmoNode.active = false;
            this.noseMianMoSpine.node.active = true;
            this.noseMianMoSpine.setAnimation(0,"01",false);
            this.noseMianMoSpine.setCompleteListener(()=>{
                this._point ++;
                this._isPlay = false;
                this.mianmoNode.active = true;
                this.noseSelect.active = false;
                this.chinSelect.active = true;
                this.mianmoNode.x = 300;
                this.mianmoNode.y = -400;
            });
        }
    };
    //下巴贴面膜
    dealTwoPoint(worldPoint){
        let localPoint = this.mianmoNode.parent.convertToNodeSpaceAR(worldPoint);
        this.mianmoNode.x = localPoint.x;
        this.mianmoNode.y = localPoint.y;
        let peng = this.chinSelect.getBoundingBoxToWorld();
        let box = this.mianmoNode.getBoundingBoxToWorld();
        if (peng.intersects(box)) {
            this._isPlay = true;
            this.mianmoNode.active = false;
            this.chinMianMoSpine.node.active = true;
            this.chinMianMoSpine.setAnimation(0,"01",false);
            this.chinMianMoSpine.setCompleteListener(()=>{
                this._point ++;
                this._isPlay = false;
                this.chinSelect.active = false;
                this.zhenNode.active = true;
                this.acneNode1.active = true;
            });
        }
    };
    //祛痘
    dealThreePoint(worldPoint){
        let localPoint = this.zhenNode.parent.convertToNodeSpaceAR(worldPoint);
        this.zhenNode.x = localPoint.x;
        this.zhenNode.y = localPoint.y;
        let peng = this.zhenPengNode.getBoundingBoxToWorld();
        let box = this.acneNode1.getBoundingBoxToWorld();
        if (peng.intersects(box)) {
            this._isPlay = true;
            this.zhenNode.active = false;
            this.acneSpine1.node.active = true;
            this.acneSpine1.setAnimation(0,"01",false);
            this.acneSpine1.setCompleteListener(()=>{
                this._point ++;
                this._isPlay = false;
                this.acneNode1.active = false;
                this.acneNode2.active = true;
                this.zhenNode.active = true;
                this.zhenNode.x = 300;
                this.zhenNode.y = -400;
            });
        }
    };
    //祛痘
    dealFourPoint(worldPoint){
        let localPoint = this.zhenNode.parent.convertToNodeSpaceAR(worldPoint);
        this.zhenNode.x = localPoint.x;
        this.zhenNode.y = localPoint.y;
        let peng = this.zhenPengNode.getBoundingBoxToWorld();
        let box = this.acneNode2.getBoundingBoxToWorld();
        if (peng.intersects(box)) {
            this._isPlay = true;
            this.zhenNode.active = false;
            this.acneSpine2.node.active = true;
            this.acneSpine2.setAnimation(0,"01",false);
            this.acneSpine2.setCompleteListener(()=>{
                this._point ++;
                this._isPlay = false;
                this.acneNode2.active = false;
                this.noseSelect.active = true;
                this.noseSiTipsNode.active = true;
            });
        }
    };

    onClickEnd(isSkip){
        let next = this.params.next && this.params.next.shift();
        if (next) {
            UIHelper.openUI(next, {
                next: this.params.next
            })
            this.node.destroy();
        }
    };

    onClickSkip(){
        this.onClickEnd(true);
    };

}
